package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.FlightSalespolicy;
import com.ktgj.flight.service.IFlightSalespolicyService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 机票销售政策维护Controller
 * 
 * @author ruoyi
 * @date 2022-06-06
 */
@RestController
@RequestMapping("/salespolicy")
public class FlightSalespolicyController extends BaseController
{
    @Autowired
    private IFlightSalespolicyService flightSalespolicyService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;


    /**
     * 查询机票销售政策维护列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody FlightSalespolicy flightSalespolicy)
    {
        startPage();
        List<FlightSalespolicy> list = flightSalespolicyService.selectFlightSalespolicyList(flightSalespolicy);
        return getDataTable(list);
    }

    /**
     * 导出机票销售政策维护列表
     */
    @PreAuthorize(hasPermi = "system:salespolicy:export")
    @Log(title = "机票销售政策维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightSalespolicy flightSalespolicy) throws IOException
    {
        List<FlightSalespolicy> list = flightSalespolicyService.selectFlightSalespolicyList(flightSalespolicy);
        ExcelUtil<FlightSalespolicy> util = new ExcelUtil<FlightSalespolicy>(FlightSalespolicy.class);
        util.exportExcel(response, list, "机票销售政策维护数据");
    }

    /**
     * 获取机票销售政策维护详细信息
     */
//    @PreAuthorize(hasPermi = "system:salespolicy:query")
    @PostMapping("/getInfo")
    public AjaxResult getInfo(@RequestBody Map<String, Object> map) {
        Long policyId = Long.valueOf(map.get("policyId").toString());
        FlightSalespolicy flightSalespolicy = flightSalespolicyService.selectFlightSalespolicyById(policyId);
        return AjaxResult.success(flightSalespolicy);
    }

    /**
     * 新增机票销售政策维护
     */
//    @PreAuthorize(hasPermi = "system:salespolicy:add")
//    @Log(title = "机票销售政策维护", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(HttpServletRequest request, @RequestBody FlightSalespolicy flightSalespolicy) {
        int i;
        try {
            String token = request.getHeader("token");
            R<Employee> loginEmployeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);

            i = flightSalespolicyService.insertFlightSalespolicy(loginEmployeeByToken.getData(), flightSalespolicy);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("新增成功");
        }
        else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改机票销售政策维护
     */
//    @PreAuthorize(hasPermi = "system:salespolicy:edit")
//    @Log(title = "机票销售政策维护", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(HttpServletRequest request, @RequestBody FlightSalespolicy flightSalespolicy) {
        String token = request.getHeader("token");
        R<Employee> loginEmployeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);

        int i = flightSalespolicyService.updateFlightSalespolicy(loginEmployeeByToken.getData(), flightSalespolicy);
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除机票销售政策维护
     */
    @PreAuthorize(hasPermi = "system:salespolicy:remove")
    @Log(title = "机票销售政策维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{policyIds}")
    public AjaxResult remove(@PathVariable Long[] policyIds)
    {
        return toAjax(flightSalespolicyService.deleteFlightSalespolicyByIds(policyIds));
    }

    /**
     * 删除机票销售政策
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, Long> map) {
        return toAjax(flightSalespolicyService.delete(map.get("policyId")));
    }
}
